{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cbaa881a",
   "metadata": {},
   "source": [
    "**RSI（相对强弱指数，Relative Strength Index）** 是由韦尔斯·怀尔德（Welles Wilder）在1978年提出的技术指标，用于衡量资产价格的**超买超卖状态**和**价格变动动量**。以下是其核心要点和应用方法：\n",
    "\n",
    "---\n",
    "\n",
    "### **一、RSI的计算原理**\n",
    "1. **公式**：  \n",
    "\\[\n",
    "   RSI = 100 - 100/(1 + RS)  注：（RS = 平均涨幅 / 平均跌幅）\n",
    "\\]\n",
    "   - **默认周期**：14日（可调整）。\n",
    "   - **平均涨幅**：过去N日上涨幅度的平均值。\n",
    "   - **平均跌幅**：过去N日下跌幅度的绝对值平均值。\n",
    "\n",
    "2. **计算步骤**（以14日为例）：  \n",
    "   1. 计算每日价格变化（今日收盘价 - 昨日收盘价）。  \n",
    "   2. 分离上涨日（变化>0）和下跌日（变化<0）。  \n",
    "   3. 计算14日平均涨幅（AU）和平均跌幅（AD）。  \n",
    "   4. RS = AU / AD，代入公式得RSI值。\n",
    "\n",
    "---\n",
    "\n",
    "### **二、RSI的实战意义**\n",
    "#### **1. 超买超卖信号**\n",
    "| RSI值范围      | 市场状态   | 操作提示               |\n",
    "|----------------|------------|------------------------|\n",
    "| **RSI > 70**   | 超买区域   | 警惕回调，可考虑减仓   |\n",
    "| **RSI < 30**   | 超卖区域   | 关注反弹，可考虑低吸   |\n",
    "| **RSI 30-70**  | 正常波动   | 趋势延续，观望或跟随   |\n",
    "\n",
    "**注意**：  \n",
    "- 在**强势趋势行情**中，RSI可能长时间处于超买（如牛市）或超卖（如熊市）区域，需结合趋势判断。\n",
    "\n",
    "#### **2. 背离信号（重要反转预警）**\n",
    "- **顶背离**：价格创新高，但RSI未创新高 → 暗示上涨动力减弱，可能见顶。  \n",
    "- **底背离**：价格创新低，但RSI未创新低 → 暗示下跌动能衰竭，可能见底。\n",
    "\n",
    "#### **3. 交叉信号**\n",
    "- **RSI上穿30**：超卖区反弹，短期买入信号。  \n",
    "- **RSI下穿70**：超买区回落，短期卖出信号。\n",
    "\n",
    "---\n",
    "\n",
    "### **三、RSI参数优化**\n",
    "1. **周期调整**：  \n",
    "   - **短线交易**：缩短周期（如RSI 7或9），提高灵敏度。  \n",
    "   - **长线投资**：延长周期（如RSI 21或30），过滤噪音。\n",
    "\n",
    "2. **多周期结合**：  \n",
    "   - 同时观察**RSI(7)**和**RSI(14)**：  \n",
    "     - 若RSI(7)上穿RSI(14)，短线动能增强。  \n",
    "     - 若RSI(7)下穿RSI(14)，动能减弱。\n",
    "\n",
    "---\n",
    "\n",
    "### **四、RSI的局限性**\n",
    "1. **趋势行情中的钝化**：  \n",
    "   - 在单边上涨/下跌趋势中，RSI可能长期超买/超卖，需结合趋势指标（如MACD、均线）过滤信号。\n",
    "\n",
    "2. **震荡市更有效**：  \n",
    "   - 在箱体震荡或横盘行情中，RSI超买超卖信号更可靠。\n",
    "\n",
    "3. **需验证背离**：  \n",
    "   - 背离信号需等待价格确认（如顶背离后出现大阴线）再行动。\n",
    "\n",
    "---\n",
    "\n",
    "### **五、RSI的实战案例**\n",
    "#### **案例1：超卖反弹（比特币日线）**  \n",
    "- 2023年1月，比特币跌至16,000美元，RSI(14)触及25后反弹，随后价格回升至23,000美元。  \n",
    "- **关键动作**：RSI上穿30 + 阳线放量确认。\n",
    "\n",
    "#### **案例2：顶背离预警（贵州茅台周线）**  \n",
    "- 2021年2月，茅台股价创2600元新高，但RSI(14)未突破前高，形成顶背离，随后股价回调至1500元。  \n",
    "- **关键动作**：背离后跌破20日均线离场。\n",
    "\n",
    "---\n",
    "\n",
    "### **六、RSI与其他指标的结合**\n",
    "1. **RSI + 移动平均线**：  \n",
    "   - 价格在200日均线上方 + RSI>50 → 顺势做多。\n",
    "\n",
    "2. **RSI + 布林带**：  \n",
    "   - RSI超卖 + 价格触及布林带下轨 → 反弹概率高。\n",
    "\n",
    "3. **RSI + MACD**：  \n",
    "   - RSI底背离 + MACD金叉 → 强反转信号。\n",
    "\n",
    "---\n",
    "\n",
    "### **七、代码实现（Python示例）**\n",
    "```python\n",
    "import pandas as pd\n",
    "import talib\n",
    "\n",
    "# 获取价格数据（示例）\n",
    "data = pd.read_csv('stock.csv', parse_dates=['date'])\n",
    "close_prices = data['close']\n",
    "\n",
    "# 计算RSI(14)\n",
    "rsi_14 = talib.RSI(close_prices, timeperiod=14)\n",
    "\n",
    "# 标记超买超卖区\n",
    "data['overbought'] = rsi_14 > 70\n",
    "data['oversold'] = rsi_14 < 30\n",
    "```\n",
    "\n",
    "---\n",
    "\n",
    "**总结**：  \n",
    "RSI是技术分析中的“温度计”，擅长捕捉短期反转机会，但需配合趋势分析及量价验证。**口诀**：  \n",
    "> 超买超卖看区间，背离信号更优先；  \n",
    "> 单边行情要谨慎，多指标合胜率添。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "67cd69c9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d1bf083b1d6c4a2a9bf3ae13d82971c2",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "1ece497c0f0241f59e8b21a4d43b9032",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "f451db9debb04b8181b238190bb6a326",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "2e969205480d41aeae15c139b9621747",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "0eac5b2daf6c4b5db37ec7c515e3cbad",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "5a91a72c55e74098b3810593af16cc12",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "1d76611c8d3a4e989b5275afd780e65c",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "2b1cfdddeb59434298217c982ee06a3c",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "65f4300de9d54bcabf9d0f3bdd8b499a",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "2ae4297a35b644229330988b8550f77f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "71ddaabb793643d1b0cceddc409c25a8",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "4f2e051add0e41d9b95f6535faa43677",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "f8e0b49ea5fe4811afc3f1d5fddd91f7",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e3ffaee804f94f11a06baa4ded565bd5",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e1e100ac2c174aa0aaf39631c5009025",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "79aca7b4183d496bac2b04147207f801",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e4239be30ee84a5c8e6959e0117afbcf",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "15aa29ac354e4c6abc9ff5b085ba1b06",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "451a00b2d1d04ffba3b3516944200176",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "13525f095dc04932995496e9a67900f7",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a4731b1d3b004724ba467b4c1970f3bc",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "1045fe5c3bd442cdbcf3857fb81ace1c",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "da6d6927f3c74b2d8493d93aa3b8e6b8",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "096105a60e7442b489d2d017f5e8e58f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "66b02a8404b4461fbbc1b19efe84c7eb",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "dc8a98ea6b1343478d8a24b818c1c81c",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "bf862aa488064c5588a12a2be541565c",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "f7fd4dfea7594a66b92865747a329263",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "3949d46702ad48fcad2b3b5b3e39d052",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "5312b0a029dc46ebb2a6851e84656c97",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "5dfb2549d6464f6ca88c9f8c7a439260",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "10237cb99bac422fa148a7ce2b8b1d81",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "217322fd708e4ddca10e68a5f27a332f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "8873e768ff064fa9a6cb3581b1968c2d",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "65a2bec9c3784e50974b93890e69ec82",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "08250f187dad4dfabc3376bd6ff804ec",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "4f9b18c78eea4844a97617fcde3d0a9a",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "639ba855cc29467ab9da04049f810729",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ec71da420a4449e99eec28d44c413b76",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d53678a915fc4ed181d9ebf1d392493d",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ce65620a4fc149fea42b93929fb05470",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "76272c57ea1d4734ab5223291c67d28a",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "7eea71f7b09a4825b746581561df6242",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "634319ad02e640d3bc1f232ef455ec01",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b5d314fd045745038e6fb4842b458b88",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "bb5f7bc89dc0483a9b7194d28651fd4c",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "fa58ad4030da4e77b96cabdcfb65f45e",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "88fe20375e674714bf99fc6b9c56f08c",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b3409e97e4e64bffb80b154d0b6a81a2",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "6adb5fd05ab44df9abe5778898ae5d8b",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "004a1eda5aa14ce8b592f2e57e9643fa",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "5569c263ca29428085b8b8d133779e33",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "cb8c9c6216e14e0f97c0b5f48bcd3a6f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "99b5fed14b6c4763b49293fd716459c2",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "791bf48b462e48928ba67ad29c2554d8",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "867aae0323934037932496390a5890f9",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "c84d549a3bc14c4dac65f74fcb61d941",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "f8cbdfea6f8c4d199b534d41d8f32998",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "2d9b3b4826cf440ba25c0479f517b87e",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "628cd034c32e48cfa20d0a259170badc",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ce1ca60c59cf40d9a9641e6b251ee061",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "1a503a2b5a3c4a75a3538212912b6c6b",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a0b3837eaaae486db30b67aec2437569",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "35e2d1b15b514aaaa67c533de116400b",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "88e9b2b1c11c415ea100e9a1228e6f43",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a3f2a72cd513416abb6ca3840e54cbf7",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ff9083e5a2dd438e8826707fb31aa894",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d5491f83523a47c69cb1da1f04673b87",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d9f9512e1a4d4dcfb722f89d9ce8a5f0",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "abd7adda54fc44ecad3a11b9aad0a14a",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "5a1582ac240f49818ed5701748afc4ec",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "6f34432af1da4299986b5371f6e7bcb7",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "151e5840c9784bfbb7ba2f356285932e",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "304f937485b94805b6fe5634ec7db121",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e18df2e7c4fe49c08f5f0ecad77820dd",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a9cdbee48fae48049ee64cf3b70dc53a",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "68ed7665160b48fe84188304edf5c959",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d555819a10e644c3bec8152cf6a5e122",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ea7d7ca8cbab4dfca1a0193b62828ba0",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "9b66e5dd19e74c249d7de3d4d2388fb6",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "08b1bb91e58648eb90efcb18e2edee46",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "76b2673c481148cd800f8078e8554725",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ca8857eaa067425b998eead3eb9329ef",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ef21e8d25c254ca8abbf3598acdf97de",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "f7588116a8af4c3e88dc6344bee82140",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "10cd470f8b2e410fba395ef6447094c2",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "97145f8598e14585aac0c0db1970c11b",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e810fba3dfbe4a91b314bac4e31e9232",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b6a5b3d868644d25aa5d5d93e8800aa4",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "873d84793ff84c65ae0f4c002d5c1e51",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "RSI 最低的前20个板块（相对低位）：\n",
      "        板块       当前价  RSI-14\n",
      "0      半导体   8481.11   42.62\n",
      "1     通信服务   2698.10   50.82\n",
      "2     影视院线   1330.17   50.83\n",
      "3     软件开发   1952.12   51.04\n",
      "4    电子化学品  35750.76   53.41\n",
      "5    旅游及酒店   2934.90   53.73\n",
      "6   其他社会服务  16953.09   54.79\n",
      "7     IT服务  11274.95   55.00\n",
      "8       白酒   2950.77   55.31\n",
      "9    非金属材料  20248.97   56.76\n",
      "10   计算机设备   3320.15   56.86\n",
      "11    文化传媒   2620.26   57.33\n",
      "12      证券   1405.22   57.37\n",
      "13      教育   4371.84   57.51\n",
      "14     贵金属   2966.22   58.09\n",
      "15    其他电子  11439.32   58.39\n",
      "16      电机   3065.22   58.65\n",
      "17    工程机械   2562.19   58.83\n",
      "18    多元金融   1004.62   59.29\n",
      "19      零售   1914.38   60.04\n"
     ]
    }
   ],
   "source": [
    "import akshare as ak\n",
    "import pandas as pd\n",
    "import datetime\n",
    "\n",
    "# 获取所有板块名称\n",
    "industry_df = ak.stock_board_industry_name_ths()\n",
    "industry_list = industry_df['name'].tolist()\n",
    "\n",
    "# 设置时间范围：近3个月\n",
    "today = datetime.datetime.today()\n",
    "start_date = (today - pd.DateOffset(days=90)).strftime('%Y%m%d')\n",
    "end_date = today.strftime('%Y%m%d')\n",
    "\n",
    "industry_rsi_list = []\n",
    "\n",
    "for symbol in industry_list:\n",
    "    try:\n",
    "        # 获取板块指数历史数据\n",
    "        df = ak.stock_board_industry_index_ths(symbol=symbol, start_date=start_date, end_date=end_date)\n",
    "        df[\"日期\"] = pd.to_datetime(df[\"日期\"])\n",
    "        df = df.sort_values(\"日期\").reset_index(drop=True)\n",
    "        df = df.rename(columns={\"收盘价\": \"close\"})\n",
    "\n",
    "        if len(df) < 20:\n",
    "            continue  # 数据太少不计算\n",
    "\n",
    "        # 计算涨跌幅\n",
    "        df[\"delta\"] = df[\"close\"].diff()\n",
    "\n",
    "        # 涨和跌分开处理\n",
    "        df[\"gain\"] = df[\"delta\"].where(df[\"delta\"] > 0, 0.0)\n",
    "        df[\"loss\"] = -df[\"delta\"].where(df[\"delta\"] < 0, 0.0)\n",
    "\n",
    "        # 计算平均涨幅和跌幅（14日）\n",
    "        avg_gain = df[\"gain\"].rolling(window=14).mean()\n",
    "        avg_loss = df[\"loss\"].rolling(window=14).mean()\n",
    "\n",
    "        # 计算 RSI\n",
    "        rs = avg_gain / avg_loss\n",
    "        rsi = 100 - (100 / (1 + rs))\n",
    "        latest_rsi = rsi.iloc[-1]\n",
    "\n",
    "        if pd.notna(latest_rsi):\n",
    "            industry_rsi_list.append({\n",
    "                \"板块\": symbol,\n",
    "                \"当前价\": round(df[\"close\"].iloc[-1], 2),\n",
    "                \"RSI-14\": round(latest_rsi, 2)\n",
    "            })\n",
    "\n",
    "    except Exception as e:\n",
    "        print(f\"{symbol} 获取失败: {e}\")\n",
    "        continue\n",
    "\n",
    "# 转成 DataFrame\n",
    "rsi_df = pd.DataFrame(industry_rsi_list)\n",
    "\n",
    "# 从低到高排序，取 RSI 最低的前10个板块\n",
    "top10_rsi = rsi_df.sort_values(\"RSI-14\").head(20).reset_index(drop=True)\n",
    "\n",
    "print(\"\\nRSI 最低的前20个板块（相对低位）：\")\n",
    "print(top10_rsi)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
